package com.sq.partner.manager.util.excel;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;

import com.sq.partner.manager.util.IdCardUtil;
import com.sq.partner.manager.util.PatternUtil;

public class ExcelTool {
	
	/**
	 * 获取cell的值
	 * @param cell
	 * @return
	 */
	public static Object getCell(HSSFCell cell){
		Object val = null;
		if(cell != null){
			int cellTyle = cell.getCellType();
			switch(cellTyle){
				case HSSFCell.CELL_TYPE_STRING :
					val = cell.getStringCellValue();
					break;
				case HSSFCell.CELL_TYPE_NUMERIC:
					val = cell.getNumericCellValue();
					break;
				case HSSFCell.CELL_TYPE_BOOLEAN:
					val = cell.getBooleanCellValue();
					break;
				case HSSFCell.CELL_TYPE_ERROR:
					val = cell.getErrorCellValue();
					break;
				case HSSFCell.CELL_TYPE_FORMULA:
					val = cell.getCellFormula();
			}
		}
		return val;
	}
	
	/**
	 * cell校验 
	 * @param rule 0不校验 1非空校验 2数字校验 3身份证校验 4日期校验  5电话校验 6日期校验可以为空 7数字校验可以为空
	 * @param cellText
	 * @return
	 */
	public static boolean checkCell(int rule, Object cellText) {
		boolean bflag = true;
		switch(rule){
			case 1:
				if(cellText == null || "".equals(String.valueOf(cellText).trim())) bflag = false;
				break;
			case 2:
				if(cellText == null || "".equals(String.valueOf(cellText).trim())){
					bflag = false;
				}else{
					Pattern pattren = Pattern.compile("\\d+(\\.\\d+)?");
					Matcher matcher = pattren.matcher(String.valueOf(cellText));
					if(!matcher.matches()) bflag = false;
				}
				break;
			case 3:
				if(cellText == null || "".equals(String.valueOf(cellText).trim())){
					bflag = false;
				}else{
					bflag = IdCardUtil.clickCardNumber(String.valueOf(cellText));
				}
				break;
			case 4:
				if(cellText == null || "".equals(String.valueOf(cellText).trim())){
					bflag = false;
				}else{
					bflag = PatternUtil.dateNumber(String.valueOf(cellText).trim());
				}
				break;
			case 5:
				if(cellText == null || "".equals(String.valueOf(cellText).trim())){
					bflag = false;
				}else{
					bflag = PatternUtil.telNumber(String.valueOf(cellText).trim());
				}
				break;
			case 6:
				if(cellText != null && !"".equals(String.valueOf(cellText).trim())){
					bflag = PatternUtil.dateNumber(String.valueOf(cellText).trim());
				}
				break;
			case 7:
				if(cellText != null && !"".equals(String.valueOf(cellText).trim())){
					Pattern pattren = Pattern.compile("\\d+(\\.\\d+)?");
					Matcher matcher = pattren.matcher(String.valueOf(cellText));
					if(!matcher.matches()) bflag = false;
				}
		}
		return bflag;
	}
	
	/**
	 * 根据规则返回错误提示
	 * @param rule 0不校验 1非空校验 2数字校验 3身份证校验 4日期校验  5电话校验 6日期校验可以为空 7数字校验可以为空 20不重复 21数量上线
	 * @return
	 */
	public static String getErrorMessage(int rule) {
		String backMes = "";
		switch(rule){
			case 1:
				backMes = "不能为空";
				break;
			case 2:
				backMes = "数字格式有误";
				break;
			case 3:
				backMes = "身份证信息有误";
				break;
			case 4:
				backMes = "日期格式有误";
				break;
			case 5:
				backMes = "电话格式有误";
				break;
			case 6:
				backMes = "日期格式有误";
				break;
			case 7:
				backMes = "数字格式有误";
				break;
			case 20:
				backMes = "数据重复";
				break;
			case 21:
				backMes = "excel文件上传内容最大条数:";
		}
		return backMes;
	}
	
	/**
	 * 检验一行数据是不是都为空，如果全为空返回true,否则返回false
	 * @param row
	 * @param colTotal
	 * @return
	 */
	public static boolean checkAllRowNull(HSSFRow row,int colTotal){
		boolean cFlag = false;
		Object cellText;
		int count = 0;
		for(int colNum = 0; colNum<colTotal; colNum++){
			cellText = getCell(row.getCell(colNum));
			if(cellText == null || "".equals(String.valueOf(cellText).trim())) count++;
		}
		if(count == colTotal){
			cFlag = true;
		}
		return cFlag;
	}
}
